﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using BLL;
using DAL;
using Models;
using System.Data;
using System.Text;
using System.Xml;
using Gift.WebApp;


public partial class backstage_delivery_way : System.Web.UI.Page
{
    // Fields
    protected DeliveryWay dw = new DeliveryWay();
    protected DeliveryWayManager dwm = new DeliveryWayManager();

    protected LogisticsCompanyManager lcm = new LogisticsCompanyManager();

    // Methods
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string pathAndQuery = HttpContext.Current.Request.Url.PathAndQuery;
        try
        {
            this.dw.DeliveryWayName = this.txtDeliveryWayName.Value;
            this.dw.DefaultLogisticsCompany = Convert.ToInt32(base.Request.Form["logisticsCompany"]);
            this.dw.SortOrder = Convert.ToInt32(this.txtSortOrder.Value);
            this.dw.Introduce = this.txtaIntroduce.Value;
            if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
            {
                this.dw.Id = Convert.ToInt32(base.Request.QueryString["id"]);
                if (this.dwm.ModifyDeliveryWay(this.dw))
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新成功！\");location.href=\"delivery_way_list.aspx\";</script>");
                }
                else
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>selectIsExitItem(\"selectLogisticsCompany\",\"" + base.Request.Form["logisticsCompany"] + "\");alert(\"更新失败！\");</script>");
                }
            }
            else
            {
                this.dw.LanguageVersion = this.Session["language_version"].ToString();
                if (this.dwm.AddDeliveryWay(this.dw))
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"添加成功！\");location.href=\"delivery_way_list.aspx\";</script>");
                }
                else
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>selectIsExitItem(\"selectLogisticsCompany\",\"" + base.Request.Form["logisticsCompany"] + "\");alert(\"添加失败！\");</script>");
                }
            }
        }
        catch
        {
            if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
            {
                base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>selectIsExitItem(\"selectLogisticsCompany\",\"" + base.Request.Form["logisticsCompany"] + "\");alert(\"更新失败！\");</script>");
            }
            else
            {
                base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>selectIsExitItem(\"selectLogisticsCompany\",\"" + base.Request.Form["logisticsCompany"] + "\");alert(\"添加失败！\");</script>");
            }
        }
    }

    private void DeliveryWayBind()
    {
        try
        {
            DataTable deliveryWayById = this.dwm.GetDeliveryWayById(Convert.ToInt32(base.Request.QueryString["id"]));
            if (deliveryWayById.Rows.Count > 0)
            {
                DataRow row = deliveryWayById.Rows[0];
                this.txtDeliveryWayName.Value = row["deliveryWayName"].ToString();
                this.txtSortOrder.Value = row["sortOrder"].ToString();
                base.ClientScript.RegisterStartupScript(base.GetType(), "", "<script type=\"text/javascript\">selectIsExitItem(\"selectLogisticsCompany\",\"" + row["defaultLogisticsCompany"].ToString() + "\");</script>");
                this.txtaIntroduce.Value = row["introduce"].ToString();
            }
        }
        catch
        {
        }
    }

    protected StringBuilder GetTitle()
    {
        StringBuilder builder = new StringBuilder();
        try
        {
            if ((base.Request.QueryString["ot"] != null) && (base.Request.QueryString["id"] != null))
            {
                builder.AppendLine("编辑方式");
                return builder;
            }
            builder.AppendLine("添加方式");
        }
        catch
        {
        }
        return builder;
    }

    protected StringBuilder LogisticsCompanyBind()
    {
        StringBuilder builder = new StringBuilder();
        try
        {
            DataTable allLogisticsCompany = this.lcm.GetAllLogisticsCompany();
            foreach (DataRow row in allLogisticsCompany.Rows)
            {
                builder.AppendLine(string.Concat(new object[] { "<option value=\"", row["id"], "\">", row["logisticsCompanyName"], "</option>" }));
            }
            return builder;
        }
        catch
        {
            return builder;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if ((HttpContext.Current.Session["admin_user"] == null) && (HttpContext.Current.Session["language_version"] == null))
        {
            base.ClientScript.RegisterStartupScript(base.GetType(), "sJs", "top.location=\"login.html\";", true);
        }
        if ((!base.IsPostBack && (base.Request.QueryString["ot"] != null)) && ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null)))
        {
            this.DeliveryWayBind();
        }
    }

}